package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.pojo.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.common.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ghy
 * @version 1.0.1
 * @date 2024-02-22 15:52:26
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "后台菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> list(Long categoryId){
        return dishService.findByCategoryId(categoryId);
    }

    @ApiOperation("修改菜品")
    @PutMapping
    public Result<String> update(@RequestBody DishDTO dishDTO){
        log.warn("修改菜品：{}", dishDTO);
        return dishService.updateWithFlavors(dishDTO);
    }

    @ApiOperation("根据ID查询菜品及口味数据")
    @GetMapping("/{id}")
    public Result<DishVO> findById(@PathVariable(name = "id") Long id){
        log.warn("根据ID查询菜品及口味数据,id:{}", id);
        return dishService.findByIdWithFlavors(id);
    }

    /**
     * 菜品批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("菜品批量删除：{}", ids);
        dishService.deleteBatch(ids);//后绪步骤实现
        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);//后绪步骤定义
        return Result.success(pageResult);
    }

    @ApiOperation("新增菜品及口味")
    @PostMapping
    public Result<String> add(@RequestBody DishDTO dishDTO){
        return dishService.addWithFlavors(dishDTO);
    }

}